﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Text.RegularExpressions;

namespace AliCMS.SubscribeModule.OleDb
{
    public partial class Company : OleDbBase, Dal.ICompany
    {
        public int GetInnerIDFromGuid(Guid guid)
        {
            Command.Parameters.Clear();
            Command.Parameters.Add("@" + Alias.Company.ID, MapType(Alias.Company.ID)).Value = guid;
            Command.CommandText = "SELECT " + MapColumn(Alias.Company.InnerID) + " FROM " + MapTable(Alias.Objects.Company) + " WHERE " + MapColumn(Alias.Company.ID) + "=?";
            object result = Command.ExecuteScalar();
            if (result == null)
                return 0;
            else
                return (int)result;
        }
        public Guid GetGuidFromInnerID(object id)
        {
            Command.Parameters.Clear();
            Command.Parameters.Add("@" + Alias.Company.InnerID, MapType(Alias.Company.InnerID)).Value = id;
            Command.CommandText = "SELECT " + MapColumn(Alias.Company.ID) + " FROM " + MapTable(Alias.Objects.Company) + " WHERE " + MapColumn(Alias.Company.InnerID) + "=?";
            object result = Command.ExecuteScalar();
            if (result == null)
                return Guid.Empty;
            else
                return (Guid)result;
        }
        public Dictionary<string, object> Get(object id)
        {
            Dictionary<string, object> result = new Dictionary<string, object>();

            StringBuilder sql = new StringBuilder("SELECT TOP 1");
            sql.Append(" " + MapColumn(Alias.Company.ID));
            sql.Append("," + MapColumn(Alias.Company.InnerID));
            sql.Append("," + MapColumn(Alias.Company.Name));
            sql.Append("," + MapColumn(Alias.Company.IndustryID));
            sql.Append("," + MapColumn(Alias.Company.IndustryName));
            sql.Append("," + MapColumn(Alias.Company.RegionID));
            sql.Append("," + MapColumn(Alias.Company.RegionName));
            sql.Append("," + MapColumn(Alias.Company.TagNames));
            sql.Append("," + MapColumn(Alias.Company.TagInnerIDs));
            sql.Append("," + MapColumn(Alias.Company.Address));
            sql.Append("," + MapColumn(Alias.Company.Zip));
            sql.Append("," + MapColumn(Alias.Company.TypeName));
            sql.Append("," + MapColumn(Alias.Company.FoundedDate));
            sql.Append("," + MapColumn(Alias.Company.LicenseNumber));
            sql.Append("," + MapColumn(Alias.Company.Owner));
            sql.Append("," + MapColumn(Alias.Company.Capital));
            sql.Append("," + MapColumn(Alias.Company.BusinessScope));
            sql.Append("," + MapColumn(Alias.Company.Homepage));
            sql.Append("," + MapColumn(Alias.Company.Contact));
            sql.Append("," + MapColumn(Alias.Company.Phone));
            sql.Append("," + MapColumn(Alias.Company.Mobile));
            sql.Append("," + MapColumn(Alias.Company.Fax));
            sql.Append("," + MapColumn(Alias.Company.Email));
            sql.Append("," + MapColumn(Alias.Company.QQ));
            sql.Append("," + MapColumn(Alias.Company.WW));
            sql.Append("," + MapColumn(Alias.Company.MoreContact));
            sql.Append("," + MapColumn(Alias.Company.Logo));
            sql.Append("," + MapColumn(Alias.Company.Staff));
            sql.Append("," + MapColumn(Alias.Company.Model));
            sql.Append("," + MapColumn(Alias.Company.Description));
            sql.Append("," + MapColumn(Alias.Company.IsDeleted));
            sql.Append("," + MapColumn(Alias.Company.CreatorID));
            sql.Append("," + MapColumn(Alias.Company.Creator));
            sql.Append("," + MapColumn(Alias.Company.CreateTime));
            sql.Append("," + MapColumn(Alias.Company.UpdatorID));
            sql.Append("," + MapColumn(Alias.Company.Updator));
            sql.Append("," + MapColumn(Alias.Company.UpdateTime));
            sql.Append(" FROM " + MapTable(Alias.Objects.Company));

            if (id is Guid)
            {
                Command.CommandText = sql + " WHERE " + MapColumn(Alias.Company.ID) + "=?";

                Command.Parameters.Clear();
                Command.Parameters.Add("@" + Alias.Company.ID, MapType(Alias.Company.ID)).Value = id;
            }
            else
            {
                Command.CommandText = sql + " WHERE " + MapColumn(Alias.Company.InnerID) + "=?";

                Command.Parameters.Clear();
                Command.Parameters.Add("@" + Alias.Company.InnerID, MapType(Alias.Company.InnerID)).Value = id;
            }

            DataReader = Command.ExecuteReader();
            if (!DataReader.HasRows)
            {
                DataReader.Close();
                return null;
            }

            DataReader.Read();
            int i = 0;
            result[Alias.Company.ID] = DataReader[i++];
            result[Alias.Company.InnerID] = DataReader[i++];
            result[Alias.Company.Name] = DataReader[i++];
            result[Alias.Company.IndustryID] = DataReader[i++];
            result[Alias.Company.IndustryName] = DataReader[i++];
            result[Alias.Company.RegionID] = DataReader[i++];
            result[Alias.Company.RegionName] = DataReader[i++];
            result[Alias.Company.TagNames] = DataReader[i++];
            result[Alias.Company.TagInnerIDs] = DataReader[i++];
            result[Alias.Company.Address] = DataReader[i++];
            result[Alias.Company.Zip] = DataReader[i++];
            result[Alias.Company.TypeName] = DataReader[i++];
            result[Alias.Company.FoundedDate] = DataReader[i++];
            result[Alias.Company.LicenseNumber] = DataReader[i++];
            result[Alias.Company.Owner] = DataReader[i++];
            result[Alias.Company.Capital] = DataReader[i++];
            result[Alias.Company.BusinessScope] = DataReader[i++];
            result[Alias.Company.Homepage] = DataReader[i++];
            result[Alias.Company.Contact] = DataReader[i++];
            result[Alias.Company.Phone] = DataReader[i++];
            result[Alias.Company.Mobile] = DataReader[i++];
            result[Alias.Company.Fax] = DataReader[i++];
            result[Alias.Company.Email] = DataReader[i++];
            result[Alias.Company.QQ] = DataReader[i++];
            result[Alias.Company.WW] = DataReader[i++];
            result[Alias.Company.MoreContact] = DataReader[i++];
            result[Alias.Company.Logo] = DataReader[i++];
            result[Alias.Company.Staff] = DataReader[i++];
            result[Alias.Company.Model] = DataReader[i++];
            result[Alias.Company.Description] = DataReader[i++];
            result[Alias.Company.IsDeleted] = DataReader[i++];
            result[Alias.Company.CreatorID] = DataReader[i] == DBNull.Value ? Guid.Empty : DataReader[i]; i++;
            result[Alias.Company.Creator] = DataReader[i++];
            result[Alias.Company.CreateTime] = DataReader[i++];
            result[Alias.Company.UpdatorID] = DataReader[i] == DBNull.Value ? Guid.Empty : DataReader[i]; i++;
            result[Alias.Company.Updator] = DataReader[i++];
            result[Alias.Company.UpdateTime] = DataReader[i++];
            DataReader.Close();

            return result;
        }
        public void Add(Dictionary<string, object> item)
        {
            #region 设置RowLang
            item.Add(Alias.Company.RowLang, "|" + Lang + "|");
            #endregion

            StringBuilder names = new StringBuilder();
            StringBuilder values = new StringBuilder();

            Command.Parameters.Clear();
            PrepareParameters(names, values, item);

            Command.CommandText = "INSERT INTO " + MapTable(Alias.Objects.Company) + "(" + names + ") VALUES(" + values + ")";
            Command.ExecuteNonQuery();
        }
        public void Modify(int id, Dictionary<string, object> changes)
        {
            Command.Parameters.Clear();
            Command.Parameters.Add("@" + Alias.Company.InnerID, MapType(Alias.Company.InnerID)).Value = id;

            #region 检查并设置RowLang
            Command.CommandText = "SELECT " + MapColumn(Alias.Company.RowLang) + " FROM " + MapTable(Alias.Objects.Company) + " WHERE " + MapColumn(Alias.Company.InnerID) + "=?";
            string rowLang = Command.ExecuteScalar().ToString();
            if (!rowLang.Contains("|" + Lang + "|"))
                changes.Add(Alias.Company.RowLang, rowLang + Lang + "|");
            #endregion

            StringBuilder sql = new StringBuilder("UPDATE " + MapTable(Alias.Objects.Company) + " SET ");
            Command.Parameters.Clear();
            PrepareParameters(sql, changes);
            Command.Parameters.Add("@" + Alias.Company.InnerID, MapType(Alias.Company.InnerID)).Value = id;

            sql.Append(" WHERE " + MapColumn(Alias.Company.InnerID) + "=?");

            #region HasSelfLimitation
            if (CheckRight)
            {
                Dictionary<string, string> user = Manager.User;
                if (user != null && Convert.ToBoolean(user["HasSelfLimitation"]))
                {
                    sql.Append(" AND " + MapColumn(Alias.Company.CreatorID) + "=?");
                    Command.Parameters.Add("@" + Alias.Company.CreatorID, MapType(Alias.Company.CreatorID)).Value = new Guid(user["ID"]);
                }
            }
            #endregion

            Command.CommandText = sql.ToString();
            Command.ExecuteNonQuery();
        }
        public void Delete(int id)
        {
            Command.Parameters.Clear();
            Command.Parameters.Add("@" + Alias.Company.InnerID, MapType(Alias.Company.InnerID)).Value = id;

            StringBuilder sql = new StringBuilder("DELETE FROM " + MapTable(Alias.Objects.Company) + " WHERE " + MapColumn(Alias.Company.InnerID) + "=?");

            #region HasSelfLimitation
            if (CheckRight)
            {
                Dictionary<string, string> user = Manager.User;
                if (user != null && Convert.ToBoolean(user["HasSelfLimitation"]))
                {
                    sql.Append(" AND " + MapColumn(Alias.Company.CreatorID) + "=?");
                    Command.Parameters.Add("@" + Alias.Company.CreatorID, MapType(Alias.Company.CreatorID)).Value = new Guid(user["ID"]);
                }
            }
            #endregion

            Command.CommandText = sql.ToString();
            Command.ExecuteNonQuery();
        }
        public DataTable List(int page, int pageSize, string condition, string sort)
        {
            DataTable result = new DataTable();
            result.Columns.Add(Alias.Company.ID, MapCSType(Alias.Company.ID));
            result.Columns.Add(Alias.Company.InnerID, MapCSType(Alias.Company.InnerID));
            result.Columns.Add(Alias.Company.Name, MapCSType(Alias.Company.Name));
            result.Columns.Add(Alias.Company.IndustryID, MapCSType(Alias.Company.IndustryID));
            result.Columns.Add(Alias.Company.IndustryName, MapCSType(Alias.Company.IndustryName));
            result.Columns.Add(Alias.Company.RegionID, MapCSType(Alias.Company.RegionID));
            result.Columns.Add(Alias.Company.RegionName, MapCSType(Alias.Company.RegionName));
            result.Columns.Add(Alias.Company.TagNames, MapCSType(Alias.Company.TagNames));
            result.Columns.Add(Alias.Company.TagInnerIDs, MapCSType(Alias.Company.TagInnerIDs));
            result.Columns.Add(Alias.Company.Address, MapCSType(Alias.Company.Address));
            result.Columns.Add(Alias.Company.Zip, MapCSType(Alias.Company.Zip));
            result.Columns.Add(Alias.Company.TypeName, MapCSType(Alias.Company.TypeName));
            result.Columns.Add(Alias.Company.FoundedDate, MapCSType(Alias.Company.FoundedDate));
            result.Columns.Add(Alias.Company.LicenseNumber, MapCSType(Alias.Company.LicenseNumber));
            result.Columns.Add(Alias.Company.Owner, MapCSType(Alias.Company.Owner));
            result.Columns.Add(Alias.Company.Capital, MapCSType(Alias.Company.Capital));
            result.Columns.Add(Alias.Company.BusinessScope, MapCSType(Alias.Company.BusinessScope));
            result.Columns.Add(Alias.Company.Homepage, MapCSType(Alias.Company.Homepage));
            result.Columns.Add(Alias.Company.Contact, MapCSType(Alias.Company.Contact));
            result.Columns.Add(Alias.Company.Phone, MapCSType(Alias.Company.Phone));
            result.Columns.Add(Alias.Company.Mobile, MapCSType(Alias.Company.Mobile));
            result.Columns.Add(Alias.Company.Fax, MapCSType(Alias.Company.Fax));
            result.Columns.Add(Alias.Company.Email, MapCSType(Alias.Company.Email));
            result.Columns.Add(Alias.Company.QQ, MapCSType(Alias.Company.QQ));
            result.Columns.Add(Alias.Company.WW, MapCSType(Alias.Company.WW));
            result.Columns.Add(Alias.Company.MoreContact, MapCSType(Alias.Company.MoreContact));
            result.Columns.Add(Alias.Company.Logo, MapCSType(Alias.Company.Logo));
            result.Columns.Add(Alias.Company.Staff, MapCSType(Alias.Company.Staff));
            result.Columns.Add(Alias.Company.Model, MapCSType(Alias.Company.Model));
            result.Columns.Add(Alias.Company.Description, MapCSType(Alias.Company.Description));
            result.Columns.Add(Alias.Company.IsDeleted, MapCSType(Alias.Company.IsDeleted));
            result.Columns.Add(Alias.Company.CreatorID, MapCSType(Alias.Company.CreatorID));
            result.Columns.Add(Alias.Company.Creator, MapCSType(Alias.Company.Creator));
            result.Columns.Add(Alias.Company.CreateTime, MapCSType(Alias.Company.CreateTime));
            result.Columns.Add(Alias.Company.UpdatorID, MapCSType(Alias.Company.UpdatorID));
            result.Columns.Add(Alias.Company.Updator, MapCSType(Alias.Company.Updator));
            result.Columns.Add(Alias.Company.UpdateTime, MapCSType(Alias.Company.UpdateTime));

            #region 条件
            Command.Parameters.Clear();
            string parenthesis = String.Empty;
            string join = String.Empty;
            StringBuilder where = new StringBuilder();
            if (!String.IsNullOrEmpty(condition))
            {
                Match match = null;
                where.Append(" WHERE " + Alias.Company.RowLang + " LIKE '%|" + Lang + "|%' AND ");
                where.Append("(" + condition + ")");

                ParseCondition(where, "a");

                #region IncludeSubRegion
                match = Regex.Match(where.ToString(), @"(?<=(^|\s|\())" + Alias.Company.IncludeSubRegion + @"\s?=\s?((?<InnerID>\d+)|(?<Guid>(\{|'|'\{)?[a-fA-F0-9]{8}(-?[a-fA-F0-9]{4}){3}-?[a-fA-F0-9]{12}(\}|'|\}')?))(?=(\s|\)|$))", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                if (match.Success)
                {
                    where.Remove(match.Index, match.Length);

                    object id = null;
                    if (match.Groups["InnerID"].Success)
                    {
                        id = match.Groups["InnerID"].Value;
                    }
                    else
                    {
                        string guid = match.Groups["Guid"].Value;
                        if (guid.StartsWith("{") || guid.StartsWith("'"))
                            guid = guid.Substring(1, guid.Length - 2);

                        bool isMSSql = Connection.ConnectionString.IndexOf("SQLOLEDB", StringComparison.InvariantCultureIgnoreCase) != -1;
                        if (isMSSql)
                            guid = "'" + guid + "'";
                        else
                            guid = "{" + guid + "}";

                        Command.CommandText = "SELECT " + MapColumn(Alias.Region.InnerID) + " FROM " + MapTable(Alias.Objects.Region) + " WHERE " + MapColumn(Alias.Region.ID) + "=" + guid;
                        id = Command.ExecuteScalar();
                    }

                    parenthesis += "(";
                    join += " INNER JOIN " + MapTable(Alias.Objects.Region) + " AS r ON r." + MapColumn(Alias.Region.ID) + "=a." + MapColumn(Alias.Company.RegionID) + ")";
                    where.Insert(match.Index, "(r." + MapColumn(Alias.Region.InnerID) + "=" + id + " OR r." + MapColumn(Alias.Region.ParentChain) + " LIKE '%." + id + "' OR r." + MapColumn(Alias.Region.ParentChain) + " LIKE '%." + id + ".%')");
                }
                #endregion

                #region IncludeSubIndustry
                match = Regex.Match(where.ToString(), @"(?<=(^|\s|\())" + Alias.Company.IncludeSubIndustry + @"\s?=\s?((?<InnerID>\d+)|(?<Guid>(\{|'|'\{)?[a-fA-F0-9]{8}(-?[a-fA-F0-9]{4}){3}-?[a-fA-F0-9]{12}(\}|'|\}')?))(?=(\s|\)|$))", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                if (match.Success)
                {
                    where.Remove(match.Index, match.Length);

                    object id = null;
                    if (match.Groups["InnerID"].Success)
                    {
                        id = match.Groups["InnerID"].Value;
                    }
                    else
                    {
                        string guid = match.Groups["Guid"].Value;
                        if (guid.StartsWith("{") || guid.StartsWith("'"))
                            guid = guid.Substring(1, guid.Length - 2);

                        bool isMSSql = Connection.ConnectionString.IndexOf("SQLOLEDB", StringComparison.InvariantCultureIgnoreCase) != -1;
                        if (isMSSql)
                            guid = "'" + guid + "'";
                        else
                            guid = "{" + guid + "}";

                        Command.CommandText = "SELECT " + MapColumn(Alias.Industry.InnerID) + " FROM " + MapTable(Alias.Objects.Industry) + " WHERE " + MapColumn(Alias.Industry.ID) + "=" + guid;
                        id = Command.ExecuteScalar();
                    }

                    parenthesis += "(";
                    join += " INNER JOIN " + MapTable(Alias.Objects.Industry) + " AS i ON i." + MapColumn(Alias.Industry.ID) + "=a." + MapColumn(Alias.Company.IndustryID) + ")";
                    where.Insert(match.Index, "(i." + MapColumn(Alias.Industry.InnerID) + "=" + id + " OR i." + MapColumn(Alias.Industry.ParentChain) + " LIKE '%." + id + "' OR i." + MapColumn(Alias.Industry.ParentChain) + " LIKE '%." + id + ".%')");
                }
                #endregion
            }
            else
            {
                where.Append(" WHERE a." + MapColumn(Alias.Company.RowLang) + " LIKE '%|" + Lang + "|%'");
            }

            #region HasSelfLimitation
            //if (CheckRight)
            //{
            //    Dictionary<string, string> user = Manager.User;
            //    if (user != null && Convert.ToBoolean(user["HasSelfLimitation"]))
            //    {
            //        if (where.Length > 0)
            //            where.Append(" AND ");

            //        where.Append("a." + MapColumn(Alias.Company.CreatorID) + "=?");
            //        Command.Parameters.Add("@" + Alias.Company.CreatorID, MapType(Alias.Company.CreatorID)).Value = new Guid(user["ID"]);
            //    }
            //}
            #endregion

            #endregion

            #region 排序
            if (!String.IsNullOrEmpty(sort))
                sort = " ORDER BY " + ParseSort(sort, "a");
            else
                sort = " ORDER BY a." + MapColumn(Alias.Company.InnerID) + " DESC";
            #endregion

            #region 构造SQL语句
            StringBuilder sql = new StringBuilder("SELECT");
            if (page > 0 && pageSize > 0)
                sql.Append(" TOP " + (page * pageSize));
            sql.Append(" a." + MapColumn(Alias.Company.ID));
            sql.Append(",a." + MapColumn(Alias.Company.InnerID));
            sql.Append(",a." + MapColumn(Alias.Company.Name));
            sql.Append(",a." + MapColumn(Alias.Company.IndustryID));
            sql.Append(",a." + MapColumn(Alias.Company.IndustryName));
            sql.Append(",a." + MapColumn(Alias.Company.RegionID));
            sql.Append(",a." + MapColumn(Alias.Company.RegionName));
            sql.Append(",a." + MapColumn(Alias.Company.TagNames));
            sql.Append(",a." + MapColumn(Alias.Company.TagInnerIDs));
            sql.Append(",a." + MapColumn(Alias.Company.Address));
            sql.Append(",a." + MapColumn(Alias.Company.Zip));
            sql.Append(",a." + MapColumn(Alias.Company.TypeName));
            sql.Append(",a." + MapColumn(Alias.Company.FoundedDate));
            sql.Append(",a." + MapColumn(Alias.Company.LicenseNumber));
            sql.Append(",a." + MapColumn(Alias.Company.Owner));
            sql.Append(",a." + MapColumn(Alias.Company.Capital));
            sql.Append(",a." + MapColumn(Alias.Company.BusinessScope));
            sql.Append(",a." + MapColumn(Alias.Company.Homepage));
            sql.Append(",a." + MapColumn(Alias.Company.Contact));
            sql.Append(",a." + MapColumn(Alias.Company.Phone));
            sql.Append(",a." + MapColumn(Alias.Company.Mobile));
            sql.Append(",a." + MapColumn(Alias.Company.Fax));
            sql.Append(",a." + MapColumn(Alias.Company.Email));
            sql.Append(",a." + MapColumn(Alias.Company.QQ));
            sql.Append(",a." + MapColumn(Alias.Company.WW));
            sql.Append(",a." + MapColumn(Alias.Company.MoreContact));
            sql.Append(",a." + MapColumn(Alias.Company.Logo));
            sql.Append(",a." + MapColumn(Alias.Company.Staff));
            sql.Append(",a." + MapColumn(Alias.Company.Model));
            sql.Append(",a." + MapColumn(Alias.Company.Description));
            sql.Append(",a." + MapColumn(Alias.Company.IsDeleted));
            sql.Append(",a." + MapColumn(Alias.Company.CreatorID));
            sql.Append(",a." + MapColumn(Alias.Company.Creator));
            sql.Append(",a." + MapColumn(Alias.Company.CreateTime));
            sql.Append(",a." + MapColumn(Alias.Company.UpdatorID));
            sql.Append(",a." + MapColumn(Alias.Company.Updator));
            sql.Append(",a." + MapColumn(Alias.Company.UpdateTime));
            sql.Append(" FROM " + parenthesis + MapTable(Alias.Objects.Company) + " AS a");
            sql.Append(join + where + sort);
            Command.CommandText = sql.ToString();
            #endregion

            #region 执行SQL语句并获取记录
            int curIndex = 0, rowIndex = 0;
            int startIndex = (page - 1) * pageSize;
            DataReader = Command.ExecuteReader();
            while (DataReader.Read())
            {
                if (pageSize != -1)
                {
                    if (curIndex < startIndex)
                    {
                        curIndex++;
                        continue;
                    }

                    if (rowIndex == pageSize)
                        break;
                }

                DataRow row = result.NewRow();
                int i = 0;
                row[Alias.Company.ID] = DataReader[i++];
                row[Alias.Company.InnerID] = DataReader[i++];
                row[Alias.Company.Name] = DataReader[i++];
                row[Alias.Company.IndustryID] = DataReader[i++];
                row[Alias.Company.IndustryName] = DataReader[i++];
                row[Alias.Company.RegionID] = DataReader[i++];
                row[Alias.Company.RegionName] = DataReader[i++];
                row[Alias.Company.TagNames] = DataReader[i++];
                row[Alias.Company.TagInnerIDs] = DataReader[i++];
                row[Alias.Company.Address] = DataReader[i++];
                row[Alias.Company.Zip] = DataReader[i++];
                row[Alias.Company.TypeName] = DataReader[i++];
                row[Alias.Company.FoundedDate] = DataReader[i++];
                row[Alias.Company.LicenseNumber] = DataReader[i++];
                row[Alias.Company.Owner] = DataReader[i++];
                row[Alias.Company.Capital] = DataReader[i++];
                row[Alias.Company.BusinessScope] = DataReader[i++];
                row[Alias.Company.Homepage] = DataReader[i++];
                row[Alias.Company.Contact] = DataReader[i++];
                row[Alias.Company.Phone] = DataReader[i++];
                row[Alias.Company.Mobile] = DataReader[i++];
                row[Alias.Company.Fax] = DataReader[i++];
                row[Alias.Company.Email] = DataReader[i++];
                row[Alias.Company.QQ] = DataReader[i++];
                row[Alias.Company.WW] = DataReader[i++];
                row[Alias.Company.MoreContact] = DataReader[i++];
                row[Alias.Company.Logo] = DataReader[i++];
                row[Alias.Company.Staff] = DataReader[i++];
                row[Alias.Company.Model] = DataReader[i++];
                row[Alias.Company.Description] = DataReader[i++];
                row[Alias.Company.IsDeleted] = DataReader[i++];
                row[Alias.Company.CreatorID] = DataReader[i] == DBNull.Value ? Guid.Empty : DataReader[i]; i++;
                row[Alias.Company.Creator] = DataReader[i++];
                row[Alias.Company.CreateTime] = DataReader[i++];
                row[Alias.Company.UpdatorID] = DataReader[i] == DBNull.Value ? Guid.Empty : DataReader[i]; i++;
                row[Alias.Company.Updator] = DataReader[i++];
                row[Alias.Company.UpdateTime] = DataReader[i++];
                result.Rows.Add(row);

                rowIndex++;
            }
            DataReader.Close();
            #endregion

            return result;
        }
        public int GetRecordCount(string condition)
        {
            #region 条件
            Command.Parameters.Clear();
            string parenthesis = String.Empty;
            string join = String.Empty;
            StringBuilder where = new StringBuilder();
            if (!String.IsNullOrEmpty(condition))
            {
                Match match = null;
                where.Append(" WHERE " + Alias.Company.RowLang + " LIKE '%|" + Lang + "|%' AND ");
                where.Append("(" + condition + ")");

                ParseCondition(where, "a");

                #region IncludeSubRegion
                match = Regex.Match(where.ToString(), @"(?<=(^|\s|\())" + Alias.Company.IncludeSubRegion + @"\s?=\s?((?<InnerID>\d+)|(?<Guid>(\{|'|'\{)?[a-fA-F0-9]{8}(-?[a-fA-F0-9]{4}){3}-?[a-fA-F0-9]{12}(\}|'|\}')?))(?=(\s|\)|$))", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                if (match.Success)
                {
                    where.Remove(match.Index, match.Length);

                    object id = null;
                    if (match.Groups["InnerID"].Success)
                    {
                        id = match.Groups["InnerID"].Value;
                    }
                    else
                    {
                        string guid = match.Groups["Guid"].Value;
                        if (guid.StartsWith("{") || guid.StartsWith("'"))
                            guid = guid.Substring(1, guid.Length - 2);

                        bool isMSSql = Connection.ConnectionString.IndexOf("SQLOLEDB", StringComparison.InvariantCultureIgnoreCase) != -1;
                        if (isMSSql)
                            guid = "'" + guid + "'";
                        else
                            guid = "{" + guid + "}";

                        Command.CommandText = "SELECT " + MapColumn(Alias.Region.InnerID) + " FROM " + MapTable(Alias.Objects.Region) + " WHERE " + MapColumn(Alias.Region.ID) + "=" + guid;
                        id = Command.ExecuteScalar();
                    }

                    parenthesis += "(";
                    join += " INNER JOIN " + MapTable(Alias.Objects.Region) + " AS r ON r." + MapColumn(Alias.Region.ID) + "=a." + MapColumn(Alias.Company.RegionID) + ")";
                    where.Insert(match.Index, "(r." + MapColumn(Alias.Region.InnerID) + "=" + id + " OR r." + MapColumn(Alias.Region.ParentChain) + " LIKE '%." + id + "' OR r." + MapColumn(Alias.Region.ParentChain) + " LIKE '%." + id + ".%')");
                }
                #endregion

                #region IncludeSubIndustry
                match = Regex.Match(where.ToString(), @"(?<=(^|\s|\())" + Alias.Company.IncludeSubIndustry + @"\s?=\s?((?<InnerID>\d+)|(?<Guid>(\{|'|'\{)?[a-fA-F0-9]{8}(-?[a-fA-F0-9]{4}){3}-?[a-fA-F0-9]{12}(\}|'|\}')?))(?=(\s|\)|$))", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                if (match.Success)
                {
                    where.Remove(match.Index, match.Length);

                    object id = null;
                    if (match.Groups["InnerID"].Success)
                    {
                        id = match.Groups["InnerID"].Value;
                    }
                    else
                    {
                        string guid = match.Groups["Guid"].Value;
                        if (guid.StartsWith("{") || guid.StartsWith("'"))
                            guid = guid.Substring(1, guid.Length - 2);

                        bool isMSSql = Connection.ConnectionString.IndexOf("SQLOLEDB", StringComparison.InvariantCultureIgnoreCase) != -1;
                        if (isMSSql)
                            guid = "'" + guid + "'";
                        else
                            guid = "{" + guid + "}";

                        Command.CommandText = "SELECT " + MapColumn(Alias.Industry.InnerID) + " FROM " + MapTable(Alias.Objects.Industry) + " WHERE " + MapColumn(Alias.Industry.ID) + "=" + guid;
                        id = Command.ExecuteScalar();
                    }

                    parenthesis += "(";
                    join += " INNER JOIN " + MapTable(Alias.Objects.Industry) + " AS i ON i." + MapColumn(Alias.Industry.ID) + "=a." + MapColumn(Alias.Company.IndustryID) + ")";
                    where.Insert(match.Index, "(i." + MapColumn(Alias.Industry.InnerID) + "=" + id + " OR i." + MapColumn(Alias.Industry.ParentChain) + " LIKE '%." + id + "' OR i." + MapColumn(Alias.Industry.ParentChain) + " LIKE '%." + id + ".%')");
                }
                #endregion
            }
            else
            {
                where.Append(" WHERE a." + MapColumn(Alias.Company.RowLang) + " LIKE '%|" + Lang + "|%'");
            }

            #region HasSelfLimitation
            //if (CheckRight)
            //{
            //    Dictionary<string, string> user = Manager.User;
            //    if (user != null && Convert.ToBoolean(user["HasSelfLimitation"]))
            //    {
            //        if (where.Length > 0)
            //            where.Append(" AND ");

            //        where.Append("a." + MapColumn(Alias.Company.CreatorID) + "=?");
            //        Command.Parameters.Add("@" + Alias.Company.CreatorID, MapType(Alias.Company.CreatorID)).Value = new Guid(user["ID"]);
            //    }
            //}
            #endregion

            #endregion

            Command.CommandText = "SELECT COUNT(*) FROM " + parenthesis + MapTable(Alias.Objects.Company) + " AS a" + join + where;
            return Convert.ToInt32(Command.ExecuteScalar());
        }

        public void Replace(string property, string from, string to)
        {
            switch (property)
            {
                case "IndustryID":
                    {
                        Command.Parameters.Clear();
                        Command.Parameters.Add("@" + Alias.Industry.ID, MapType(Alias.Industry.ID)).Value = new Guid(to);

                        Command.CommandText = "SELECT " + MapColumn(Alias.Industry.Name, Lang) + " FROM " + MapTable(Alias.Objects.Industry) + " WHERE " + MapColumn(Alias.Industry.ID) + "=?";
                        DataReader = Command.ExecuteReader();
                        DataReader.Read();
                        object name = DataReader[0];
                        DataReader.Close();

                        Command.Parameters.Add("@" + Alias.Company.IndustryName, MapType(Alias.Company.IndustryName)).Value = name;
                        Command.Parameters.Add("@" + Alias.Company.IndustryID, MapType(Alias.Company.IndustryID)).Value = new Guid(from);

                        Command.CommandText = "UPDATE " + MapTable(Alias.Objects.Company) + " SET " + MapColumn(Alias.Company.IndustryID) + "=?," + MapColumn(Alias.Company.IndustryName) + "=? WHERE " + MapColumn(Alias.Company.IndustryID) + "=?";
                        Command.ExecuteNonQuery();
                    }
                    break;
                case "RegionID":
                    {
                        Command.Parameters.Clear();
                        Command.Parameters.Add("@" + Alias.Region.ID, MapType(Alias.Region.ID)).Value = new Guid(to);

                        Command.CommandText = "SELECT " + MapColumn(Alias.Region.Name, Lang) + " FROM " + MapTable(Alias.Objects.Region) + " WHERE " + MapColumn(Alias.Region.ID) + "=?";
                        DataReader = Command.ExecuteReader();
                        DataReader.Read();
                        object name = DataReader[0];
                        DataReader.Close();

                        Command.Parameters.Add("@" + Alias.Company.RegionName, MapType(Alias.Company.RegionName)).Value = name;
                        Command.Parameters.Add("@" + Alias.Company.RegionID, MapType(Alias.Company.RegionID)).Value = new Guid(from);

                        Command.CommandText = "UPDATE " + MapTable(Alias.Objects.Company) + " SET " + MapColumn(Alias.Company.RegionID) + "=?," + MapColumn(Alias.Company.RegionName) + "=? WHERE " + MapColumn(Alias.Company.RegionID) + "=?";
                        Command.ExecuteNonQuery();
                    }
                    break;
                case "Name":
                    {
                        Command.Parameters.Clear();
                        Command.Parameters.Add("@" + Alias.Company.Name, MapType(Alias.Company.Name)).Value = from;

                        OleDbDataAdapter adapter = new OleDbDataAdapter();
                        adapter.SelectCommand = Command;

                        DataTable companys = new DataTable();
                        Command.CommandText = "SELECT " + MapColumn(Alias.Company.Name, Lang) + "," + MapColumn(Alias.Company.InnerID) + " FROM " + MapTable(Alias.Objects.Company) + " WHERE " + MapColumn(Alias.Company.Name, Lang) + " LIKE '%' + ? + '%'";
                        adapter.Fill(companys);

                        Command.Parameters.Add("@" + Alias.Company.InnerID, MapType(Alias.Company.InnerID));
                        foreach (DataRow row in companys.Rows)
                        {
                            Command.Parameters[0].Value = row[0].ToString().Replace(from, to);
                            Command.Parameters[1].Value = row[1];
                            Command.CommandText = "UPDATE " + MapTable(Alias.Objects.Company) + " SET " + MapColumn(Alias.Company.Name) + "=? WHERE " + MapColumn(Alias.Company.InnerID) + "=?";
                            Command.ExecuteNonQuery();
                        }
                    }
                    break;
                case "Description":
                    {
                        Command.Parameters.Clear();
                        Command.Parameters.Add("@" + Alias.Company.Description, OleDbType.VarWChar).Value = from;//这里直接用了VarWChar类型，是因为sqlserver2005上下面的执行代码出错

                        OleDbDataAdapter adapter = new OleDbDataAdapter();
                        adapter.SelectCommand = Command;

                        DataTable companys = new DataTable();
                        Command.CommandText = "SELECT " + MapColumn(Alias.Company.Description, Lang) + "," + MapColumn(Alias.Company.InnerID) + " FROM " + MapTable(Alias.Objects.Company) + " WHERE " + MapColumn(Alias.Company.Description, Lang) + " LIKE '%' + ? + '%'";
                        adapter.Fill(companys);

                        Command.Parameters.Add("@" + Alias.Company.InnerID, MapType(Alias.Company.InnerID));
                        foreach (DataRow row in companys.Rows)
                        {
                            Command.Parameters[0].Value = row[0].ToString().Replace(from, to);
                            Command.Parameters[1].Value = row[1];
                            Command.CommandText = "UPDATE " + MapTable(Alias.Objects.Company) + " SET " + MapColumn(Alias.Company.Description) + "=? WHERE " + MapColumn(Alias.Company.InnerID) + "=?";
                            Command.ExecuteNonQuery();
                        }
                    }
                    break;
                case "Logo":
                    {
                        Command.Parameters.Clear();
                        Command.Parameters.Add("@" + Alias.Company.Logo, MapType(Alias.Company.Logo)).Value = from;

                        OleDbDataAdapter adapter = new OleDbDataAdapter();
                        adapter.SelectCommand = Command;

                        DataTable companys = new DataTable();
                        Command.CommandText = "SELECT " + MapColumn(Alias.Company.Logo) + "," + MapColumn(Alias.Company.InnerID) + " FROM " + MapTable(Alias.Objects.Company) + " WHERE " + MapColumn(Alias.Company.Logo, Lang) + " LIKE '%' + ? + '%'";
                        adapter.Fill(companys);

                        Command.Parameters.Add("@" + Alias.Company.InnerID, MapType(Alias.Company.InnerID));
                        foreach (DataRow row in companys.Rows)
                        {
                            Command.Parameters[0].Value = row[0].ToString().Replace(from, to);
                            Command.Parameters[1].Value = row[1];
                            Command.CommandText = "UPDATE " + MapTable(Alias.Objects.Company) + " SET " + MapColumn(Alias.Company.Logo) + "=? WHERE " + MapColumn(Alias.Company.InnerID) + "=?";
                            Command.ExecuteNonQuery();
                        }
                    }
                    break;
            }
        }
    }
}
